﻿using System;
using System.Drawing;
using System.Windows.Forms;
using XCI.Helper;
using XCI.Sys.Helper;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows.Extensions;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    public partial class FrmRoleDetails : FrmSysBase
    {
        private readonly GridHelper<SysRole> helper;
        private GridHelper<SysUser> userHelper;
        private TreeHelper<SysModule> moduleTreeHelper;
        private ModuleHelper moduleHelper;
        private SysRole model;
        private string id;

        public FrmRoleDetails(string p_id)
        {
            InitializeComponent();
            id = p_id;
        }

        public FrmRoleDetails(GridHelper<SysRole> p_helper)
        {
            InitializeComponent();
            helper = p_helper;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keys)
        {
            return OnEsc(msg, keys, DetailsFormEscResultCancel);
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void InitForm()
        {
            userHelper = new GridHelper<SysUser>(userGridView);
            moduleHelper = new ModuleHelper(treeListx1);
            moduleTreeHelper = new TreeHelper<SysModule>(treeListx1);

            editDept.Properties.Appearance.Font = new Font(Font.Name, Font.Size, FontStyle.Underline);
            BindModel();
        }

        /// <summary>
        /// 绑定数据到界面
        /// </summary>
        private void BindModel()
        {
            if (helper != null)
            {
                id = helper.SelectedId;
                btnPrev.Enabled = !helper.IsFirstFocus;
                btnNext.Enabled = !helper.IsLastFocus;
            }

            model = RoleService.Instance.SelectById(id);
            if (model == null) return;

            WinHelper.SetFormData(layoutPanel, model);

            //查询当前角色成员
            var userList = UserService.Instance.SelectListByRoleId(model.Id);
            userHelper.SetDataSource(userList);

            //查询当前角色关联的权限模块
            var modulesList = ModuleService.Instance.SelectModuleMapListByObjectId(model.Id);
            moduleTreeHelper.SetDataSource(modulesList);
            moduleHelper.InitModuleImageList(treeImageList, false);
            treeListx1.ExpandAll();

            //获取最后一次操作人信息
            var hmodel = HistoryService.Instance.SelectLastByPrimaryKey(id);
            if (hmodel != null)
            {
                editOperateUserName.Text = hmodel.OperateUserName;
                editOperateDateTime.Text = DateTimeHelper.FormatDateHasSecond(hmodel.OperateDateTime);
            }

            recordHistoryGridControl1.PrimaryKey = id;
        }

        private void FrmAppDetails_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        private void TreeListx1_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e)
        {
            e.NodeImageIndex = moduleHelper.GetModuleImageIndex(e.Node);
        }

        private void BtnPrev_Click(object sender, EventArgs e)
        {
            helper.View.MovePrev();
            BindModel();
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            helper.View.MoveNext();
            BindModel();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            ResultCancel();
        }

        private void EditDept_DoubleClick(object sender, EventArgs e)
        {
            var detailsForm = new FrmDeptDetails(model.DeptId);
            detailsForm.ShowDialogOk();
        }

        private void UserGridView_DoubleClick(object sender, EventArgs e)
        {
            helper.DblClickRow(e, () =>
            {
                var detailsForm = new FrmUserDetails(userHelper.SelectedId);
                detailsForm.ShowDialogOk();
            });
        }
    }
}
